<?php
/**
 * This is the FireFeed Login form.
 * 
 * @uses       Zend_Form
 * @package    FireFeed
 * @subpackage Form
 */
class Form_Login extends Zend_Form
{
	/**
     * init() routine called when Zend_Form objects are created.
     * It make sense to define form elements in this method.
     * 
     * @see    http://framework.zend.com/manual/en/zend.form.html
     * @return void
     */ 
	public function init()
    {
    	$this->setAction('/auth/login');
    	$this->setMethod('POST');
        
    	$this->setElementFilters(array('StringTrim'));
    	
    	$this->setAttrib('class', 'clearfix');
    	$this->setAttrib('id', 'loginForm');

    	// Add an openID checkbox
        $checkbox = new Zend_Form_Element_Checkbox('isOpenID');
        $checkbox->setLabel('Open ID')
        		 ->setAttrib('class', 'fieldSlider')
        		 ->setDecorators(array( 
	               array('ViewHelper', array('helper'=>'formCheckbox')),
	               array('Label', array('class'=>'grey labelOpenid')),
	               array('HtmlTag', array('tag' => 'li', 'class' => 'element-group'))
	             ));
	    
    	
        // Add Username element
        $username = new Zend_Form_Element_Text('openid_identifier');
        $username->setLabel('username')
	        	 ->setAttrib('class', 'field removeon NoHints')
    	         ->setRequired(true)
    	         ->setDecorators(array(
    	           array('ViewHelper', array('helper'=>'formText')),
    	           array('Label', array('class'=>'gray labelfield')),
    	           array('Errors'),
                   array('HtmlTag', array('tag' => 'li', 'class' => 'element-group'))
    	         ));
    	         
        // Add a password element
    	$password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password:')
        		 ->setAttrib('class', 'field removeon NoHints')
                 ->setValidators(array('alnum'))
                 ->setDecorators(array(
                   array('ViewHelper', array('helper'=>'formPassword')),
                   array('Label', array('class'=>'grey lblpwd labelfield')),
                   array('Errors'),
                   array('HtmlTag', array('tag' => 'li', 
                                          'class' => 'element-group',
                                          'id' => 'lipassword'))
                 ));
    	
        // Add OpenID action hidden field
        $openid_action = new Zend_Form_Element_Hidden('openid_action');
        $openid_action->setValue('login');
        $openid_action->setDecorators(array('ViewHelper'));
                
        // Add a submit button
        $submit = new Zend_Form_Element_Submit('Login');
        $submit->setAttrib('class', 'bt_login profileSubmit')
               ->setDecorators(array(
                   array('ViewHelper', array('helper'=>'formSubmit')),
                   array('HtmlTag', array('tag' => 'li', 'class' => 'element-group'))
               ));
        
        $this->addElements(array(
            $username,
            $password,
            $checkbox,
            $openid_action,
            $submit
        ));
        
        $this->setDecorators(array(
            'FormElements',
            'Form',
            array('HtmlTag', array('tag' => '<ul>', 'class' => 'ulForm'))
        ));
    }
    
    /**
     * Override validation to set password field requirement only for normal login
     */
    public function isValid($data)
    {
    	if (array_key_exists('isOpenID', $data) && $data['isOpenID'] == 1) {
    		$this->password->setRequired(false);
    	} else {
    		$this->password->setRequired(true);
    	}
    	
    	return parent::isValid($data);
    }
}
